package 数组题目.数组求和;

import java.util.Arrays;
import java.util.HashMap;

//和无序两数之后
class 无序两数之和 {
    public int[] twoSum(int[] nums, int target) {
        // 一个左一个右，如果左+右的值 > target 右边移动，反之左边移动
        HashMap<Integer, Integer> map = new HashMap<>(nums.length, 1);
        for (int i = 0; i < nums.length; i++) {
            map.put(nums[i], i);
        }
        for (int i = 0; i < nums.length; i++) {
            int complement = target - nums[i];
            if (map.containsKey(complement)) {
                Integer j = map.get(complement);
                if (j == i) {
                    continue;
                }
                return new int[]{i, j};
            }
        }
        return new int[2];
    }

    public static void main(String[] args) {
        System.out.println(Arrays.toString(new 无序两数之和().twoSum(new int[]{3, 2, 4}, 6)));
    }
}
